<template>
	<view>
		<scroll-view :style="{ height: swiperHeight + 'px' }" scroll-y="true" class="list" @scrolltolower="loadMore">
			<block v-for="(trend, index) in trendLists.list" :key="index">
				<view class="mine-trend-item">
					<trend-info @delete="deleteTrend" :toGo="false" :trend="trend" :isMe="true" :index="index"></trend-info>
				</view>
			</block>
			
			<!-- 加载更多 -->
			<load-more :loadText="loadText" :loading="false"></load-more>
		</scroll-view>
	</view>
</template>

<script>
import trendInfo from '../../components/common/trend-info.vue';
import loadMore from '../../components/common/load-more.vue';
import constant from '../../utils/Constant.js';
import showToast from '../../utils/ToastUtils.js';
export default {
	components: {
		trendInfo,
		loadMore
	},
	data() {
		return {
			loadText:'上拉加载更多',
			trendLists: {
				pageNo:1,
				list:[]
			}
		};
	},
	methods: {
		deleteTrend(index){
			uni.request({
			    url: constant.baseHttpUri + constant.pathUri.deleteTrend,
			    data: {
					"trendId":this.trendLists.list[index].id
				},
			    header: constant.authHead(),
				method: 'DELETE',
			    success: res => {
					if(res.data.code === constant.dataCode.OK ){
						if(res.data.data){
							this.trendLists.list.splice(index,1);
						}else{
							showToast("删除动态失败");;
						}
					}else{
						showToast(res.data.msg);
					}
			    },fail(r){
					console.log(r);
				}
			});
		},
		loadMore() {
			if (this.loadText != '上拉加载更多') {
				return;
			}
			// 修改状态
			this.loadText = '加载中...';
			// 获取数据
			this.trendLists.pageNo+=1;
			this.loadMineTrend();
		},
		loadMineTrend(){
			uni.request({
			    url: constant.baseHttpUri + constant.pathUri.mineTrend,
			    header: constant.authHead(),
				data: {
					"pageNo":this.trendLists.pageNo
				},
				method: 'GET',
			    success: res => {
					if(res.data.code === constant.dataCode.OK ){
						this.trendLists.list.push(...res.data.data);
						if(res.data.data.length<10){
							this.loadText = '没有更多数啦!';
						}else{
							this.loadText = '上拉加载更多';
						}
					}else{
						showToast(res.data.msg);
					}
			    },fail:r=>{
					this.loadText = '上拉加载更多';
					console.log(r);
				}
			});
		},
	},
	// 原生点击
	onNavigationBarButtonTap(e) {
		if (e.index === 0) {
			uni.navigateTo({
				url: `../topic/topic?type=${2}`
			});
		}
	},
	onLoad() {
		uni.getSystemInfo({
			success: res => {
				this.swiperHeight = res.windowHeight;
				this.loadMineTrend();
			}
		});
	},
};
</script>

<style lang="less" scoped>
	.mine-trend-item{
		background-color: #F5F7FA;
		padding-top:20rpx;
	}
</style>
